package com.three.iot.rest.controller.common;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.three.iot.api.UserService;
import com.three.iot.commons.dto.UserCondDto;
import com.three.iot.commons.dto.UserModiPassDto;
import com.three.iot.commons.dto.UserResultDto;
import com.three.iot.commons.enums.Result;
import com.three.iot.commons.enums.ResultCode;
import com.three.iot.rest.utils.ValidatorUtil;
import com.three.iot.rest.vo.admin.SysUserFogetPassVo;
import com.three.iot.rest.vo.admin.SysUserModiPassVo;
import com.three.iot.rest.vo.api.UserCondVo;
import com.three.iot.service.core.LoginCoreService;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author luiz
 * @Title: AbstractUserController
 * @ProjectName iot
 * @Description: TODO
 * @date 2019-04-17 17:12
 */
@Component
public abstract class AbstractUserController {
    private static final Logger logger = LoggerFactory.getLogger(AbstractUserController.class);
    @Resource
    private UserService userService;
    @Autowired
    private LoginCoreService loginCoreService;

    /**
     * 用户分页条件查询
     */
    @PostMapping("/listPage")
    @ApiOperation(value = "用户分页条件查询", notes = "用户分页条件查询")
    public Result findPage(@RequestBody UserCondVo userCondVo) {
        try {
            UserCondDto userCondDto = buildParam(userCondVo);
            Page<UserResultDto> userPage = userService.findUserPage(userCondDto);
            PageInfo pageInfo = new PageInfo(userPage);
            return Result.newSuccess(pageInfo);
        } catch (Exception e) {
            logger.error("用户分页条件查询失败:{}", e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    private UserCondDto buildParam(UserCondVo userCondVo) {
        UserCondDto userCondDto = new UserCondDto();
        BeanUtils.copyProperties(userCondVo, userCondDto);
        return userCondDto;
    }


    /**
     * 按照用户编号查询用户
     */
    @ApiOperation(value = "按照用户编号查询用户", notes = "按照用户编号查询用户")
    @GetMapping("/findByUserNo/{userNo}")
    public Result findByUserNo(@PathVariable("userNo") String userNo) {
        try {
            return userService.findByUniqIndex(userNo);
        } catch (Exception e) {
            logger.error("按照用户编号查询用户:{}", e);
            return Result.newError(ResultCode.FAIL);
        }

    }


    /**
     * 忘记密码
     *
     * @param sysUserFogetPassVo
     * @return
     */
    @ResponseBody
    @PostMapping("/forgetPass")
    @ApiOperation(value = "忘记密码", notes = "忘记密码")
    public Result forgetPass(@RequestBody SysUserFogetPassVo sysUserFogetPassVo, HttpServletRequest request) {
        try {
            //校验用户实体字段，
            ValidatorUtil.validateEntity(sysUserFogetPassVo);
            UserModiPassDto userModiPassDto = new UserModiPassDto();
            BeanUtils.copyProperties(sysUserFogetPassVo, userModiPassDto);
            return loginCoreService.forgetPass(userModiPassDto);
        } catch (Exception e) {
            logger.error("用户忘记密码异常!{}", e);
            return Result.newError(ResultCode.FAIL);
        }
    }


    /**
     * 修改密码
     *
     * @param sysUserModiPassVo
     * @return
     */
    @ResponseBody
    @PostMapping("/modiPass/{forgetRadomStr}/")
    @ApiOperation(value = "修改密码", notes = "修改密码")
    public Result modiPass(@PathVariable("forgetRadomStr") String forgetRadomStr, @RequestBody SysUserModiPassVo sysUserModiPassVo, HttpServletRequest request) {
        try {
            //校验用户实体字段，
            ValidatorUtil.validateEntity(sysUserModiPassVo);
            UserModiPassDto userModiPassDto = new UserModiPassDto();
            BeanUtils.copyProperties(sysUserModiPassVo, userModiPassDto);
            userModiPassDto.setForgetRadomStr(forgetRadomStr);
            return loginCoreService.modiPass(userModiPassDto);
        } catch (Exception e) {
            logger.error("用户修改密码异常!{}", e);
            return Result.newError(ResultCode.FAIL);
        }
    }

}
